
//------------------------------------------------------------------------------

using Apps.Models;
using Apps.IDAL.CY;
using System;
using System.Linq;

namespace Apps.DAL.CY
{
    public partial class CY_COAL_SAVERepository : BaseRepository<CY_COAL_SAVE>, ICY_COAL_SAVERepository, IDisposable
    {
        public CY_COAL_SAVERepository(DBContainer db) : base(db)
        {

        }

        public override bool Delete(CY_COAL_SAVE model)
        {
            return base.Delete(model);
        }

        public IQueryable<CY_COAL_SAVEModel> QueryList()
        {
            var query = from val in Context.V_CY_COALSAVE group val by val.SAVE_ID into q
                        select new
                        {
                            SAVE_ID = q.Key,
                            AMOUNT = q.Sum(a => a.SAVE_AMOUNT)
                        };
            var result = from a in Context.CY_COAL_SAVE
                         join d in query on a.SAVE_ID equals d.SAVE_ID into dbs
                         from db in dbs.DefaultIfEmpty()
                         join b in Context.SysUser on a.SAVE_USER equals b.Id into tbs
                         from tb in tbs.DefaultIfEmpty()
                         join c in Context.PUB_TEAM on a.TEAM_ID equals c.TEAM_ID into cbs
                         from cb in cbs.DefaultIfEmpty()
                         select new CY_COAL_SAVEModel()
                         {
                             SAVE_ID = a.SAVE_ID,
                             SAVE_DATE = a.SAVE_DATE,
                             AMOUNT = string.IsNullOrEmpty(db.SAVE_ID) ? 0 : db.AMOUNT,
                             SAVE_USER = a.SAVE_USER,
                             SAVE_TIME = a.SAVE_TIME,
                             SAVE_TYPE = a.SAVE_TYPE,
                             TEAM_ID = a.TEAM_ID,
                             SAVE_USERNAME = tb.TrueName,
                             TEAM_NAME = cb.TEAM_NAME,
                         };

            return result;
        }
    }
}
